home *** CD-ROM | disk | FTP | other *** search
- #ifdef THINK_C
- #define start main
- #endif
-
- #include <string.h>
-
- #include <Files.h>
- #include <AppleEvents.h>
- #include <Quickdraw.h>
- #include <Menus.h>
- #include <Windows.h>
- #include <ToolUtils.h>
- #include <Desk.h>
- #include <SegLoad.h>
- #include <OSUtils.h>
- #include <Memory.h>
- #include <Fonts.h>
- #include <Events.h>
- #include <OSEvents.h>
- #include <TextEdit.h>
- #include <Dialogs.h>
- #include <Aliases.h>
- #include <Folders.h>
- #include <Errors.h>
- #include <GestaltEqu.h>
- #include <StandardFile.h>
-
- OSErr GotRequiredParams(AppleEvent *);
- void scanfolder(FSSpec *);
- void main(void);
- void SetUpMenus(void);
- void AdjustMenus(void);
- void HandleMenu(long);
- void SetUpWindow(void);
- void InitMacintosh(void);
- void HandleMouseDown(EventRecord *);
- void HandleEvent(void);
- void UpdateFileName(FSSpec *name);
- void BeginProcessing(void);
- void EndProcessing(void);
- void ProcessDoc (FSSpec *curDocFSS);
- void HandleMenu (long mSelect);
- void SetUpMenus(void);
- void InitMacintosh(void);
- void HandleMouseDown (EventRecord *theEvent);
- void HandleEvent(void);
- void SetUpWindow(void);
- void ChkOsErr(OSErr);
-
- int debug = 0;
-
- TEHandle TEH;
- #ifdef PROGRESSWINDOW
- WindowPtr progressWindow;
- Rect dragRect;
- Rect windowBounds = { 40, 40, 200, 300 };
- #endif
-
- void ChkOsErr(OSErr errcode)
- {
- char errno[9],*p = &errno[sizeof(errno)];
- if (errcode < 0)
- {
- errcode = -errcode;
- while (errcode > 0)
- {
- *--p = errcode%10+'0';
- errcode /= 10;
- }
- *--p = '-';
- *--p = &errno[sizeof(errno)]-p;
- ParamText((void *)p, 0, 0, 0);
- Alert(129, 0);
- ExitToShell();
- }
- }
-
- OSErr GotRequiredParams (AppleEvent *theEvent)
- {
- DescType returnedType;
- long actualSize;
- OSErr err;
-
- err = AEGetAttributePtr (theEvent, keyMissedKeywordAttr, typeWildCard, &returnedType, NULL, 0, &actualSize);
- if (err == errAEDescNotFound)
- return noErr;
- else if (err == noErr)
- return errAEEventNotHandled;
- else
- return err;
- }
-
- void HandleOapp (long refcon, AppleEvent *theReply, AppleEvent *theAppleEvent, OSErr errplace)
- {
- errplace = noErr;
- #ifdef THINK_C
- asm {
- UNLK A6
- MOVEA.L (A7)+,A0
- LEA 12(A7),A7
- JMP (A0)
- }
- #else
- asm ("\n"
- "unlk a6\n"
- "movel a7@+,a0\n"
- "lea a7@(12),a7\n"
- "jmp a0@\n"
- );
- #endif
- }
-
- void HandleOdoc (long refcon, AppleEvent *theReply, AppleEvent *theAppleEvent, OSErr err)
- {
- AEDescList docList;
-
- err = AEGetParamDesc (theAppleEvent, keyDirectObject, typeAEList, &docList);
- if (err == noErr)
- {
- err = GotRequiredParams (theAppleEvent);
- if (err == noErr)
- {
- long numDocs, i, actualSize;
- Boolean wasAlias,isFolder;
- OSErr err;
- AEKeyword keyword;
- DescType returnedType;
-
- err = AECountItems (&docList, &numDocs);
- if (err == noErr)
- {
- BeginProcessing ();
- for (i = 1; i <= numDocs; i++)
- {
- FSSpec curDocFSS;
- err = AEGetNthPtr (&docList, i, typeFSS, &keyword, &returnedType,
- (Ptr) &curDocFSS, sizeof (FSSpec), &actualSize);
- if (err == noErr)
- {
- err = ResolveAliasFile (&curDocFSS, true, &isFolder, &wasAlias);
- if (err == noErr)
- {
- if (isFolder) scanfolder(&curDocFSS);
- else ProcessDoc(&curDocFSS);
- }
- }
- }
- err = noErr;
- EndProcessing ();
- }
- }
- (void) AEDisposeDesc (&docList);
- }
- #ifdef THINK_C
- asm {
- UNLK A6
- MOVEA.L (A7)+,A0
- LEA 12(A7),A7
- JMP (A0)
- }
- #else
- asm ("\n"
- "unlk a6\n"
- "movel a7@+,a0\n"
- "lea a7@(12),a7\n"
- "jmp a0@\n"
- );
- #endif
- }
-
- void HandlePdoc (long refcon, AppleEvent *theReply, AppleEvent *theAppleEvent, OSErr errplace)
- {
- errplace = errAEEventNotHandled;
- #ifdef THINK_C
- asm {
- UNLK A6
- MOVEA.L (A7)+,A0
- LEA 12(A7),A7
- JMP (A0)
- }
- #else
- asm ("\n"
- "unlk a6\n"
- "movel a7@+,a0\n"
- "lea a7@(12),a7\n"
- "jmp a0@\n"
- );
- #endif
- }
-
- void HandleQuit (long refcon, AppleEvent *theReply, AppleEvent *theAppleEvent, OSErr errplace)
- {
- _exit(0);
- errplace = noErr;
- #ifdef THINK_C
- asm {
- UNLK A6
- MOVEA.L (A7)+,A0
- LEA 12(A7),A7
- JMP (A0)
- }
- #else
- asm ("\n"
- "unlk a6\n"
- "movel a7@+,a0\n"
- "lea a7@(12),a7\n"
- "jmp a0@\n"
- );
- #endif
- }
-
- void ReturnEventNotHandled (long refcon, AppleEvent *theReply, AppleEvent *theAppleEvent, OSErr errplace)
- {
- errplace = errAEEventNotHandled;
- #ifdef THINK_C
- asm {
- UNLK A6
- MOVEA.L (A7)+,A0
- LEA 12(A7),A7
- JMP (A0)
- }
- #else
- asm ("\n"
- "unlk a6\n"
- "movel a7@+,a0\n"
- "lea a7@(12),a7\n"
- "jmp a0@\n"
- );
- #endif
- }
-
- OSErr CurrentProcessLocation(FSSpec *applicationSpec)
- {
- ProcessSerialNumber currentPSN;
- ProcessInfoRec info;
-
- currentPSN.highLongOfPSN = 0;
- currentPSN.lowLongOfPSN = kCurrentProcess;
- info.processInfoLength = sizeof(ProcessInfoRec);
- info.processName = NULL;
- info.processAppSpec = applicationSpec;
- return ( GetProcessInformation(¤tPSN, &info) );
- }
-
- void test_standalone(void)
- {
- FSSpec applicationSpec;
- OSErr err = CurrentProcessLocation(&applicationSpec);
- CInfoPBRec cPB;
- cPB.hFileInfo.ioNamePtr = applicationSpec.name;
- cPB.hFileInfo.ioVRefNum = applicationSpec.vRefNum;
- cPB.hFileInfo.ioDirID = applicationSpec.parID;
- cPB.hFileInfo.ioFDirIndex = 0;
-
- if (!PBGetCatInfoSync(&cPB))
- {
- long size = cPB.hFileInfo.ioFlLgLen;
- if (size)
- {
- BeginProcessing();
- ProcessDoc(&applicationSpec);
- EndProcessing();
- }
- }
- }
-
- extern long *system_vectors;
-
- void start(void)
- {
- // test_qd();
- InitMacintosh();
- probe();
- SetUpMenus();
- /* maintain compatibility with non universal headers */
- #ifndef NewAEEventHandlerProc
- #define NewAEEventHandlerProc(x) x
- #endif
-
- AEInstallEventHandler (kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(HandleOapp), 0, false);
- AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc(HandleOdoc), 0, false);
- AEInstallEventHandler (kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerProc(HandlePdoc), 0, false);
- AEInstallEventHandler (kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(HandleQuit), 0, false);
-
- test_standalone();
- #ifdef PROGRESSWINDOW
- SetUpWindow();
- #endif
- system_vectors[32] = (long)trap_patch_0();
- asm {
- moveq #0,d0
- trap #0
- };
- }
- /* end main */
-
-
-
- MenuHandle appleMenu, fileMenu, editMenu;
-
- enum {
- appleID = 1,
- fileID,
- editID
- };
-
- enum {
- openItem = 1,
- closeItem,
- quitItem = 4
- };
-
- void SetUpMenus(void)
- {
- InsertMenu(appleMenu = NewMenu(appleID, (unsigned char *)"\001\024"), 0);
- InsertMenu(fileMenu = NewMenu(fileID, (unsigned char *)"\004File"), 0);
- DrawMenuBar();
- AppendMenu(appleMenu, (unsigned char *)"\011About ...");
- AddResMenu(appleMenu, 'DRVR');
- AppendMenu(fileMenu, (unsigned char *)"\pOpen/O;Flags/L;Quit/Q;Toggle debug/D");
- }
- /* end SetUpMenus */
-
- void HandleMenu(long mSelect)
- {
- int menuID = HiWord(mSelect);
- int menuItem = LoWord(mSelect);
- Str255 name;
- GrafPtr savePort;
- WindowPeek frontWindow;
-
- switch (menuID)
- {
- case appleID:
- switch(menuItem)
- {
- case 1:
- Alert(199,0);
- break;
- default:
- GetPort(&savePort);
- GetItem(appleMenu, menuItem, name);
- OpenDeskAcc(name);
- SetPort(savePort);
- }
- break;
-
- case fileID:
- switch(menuItem)
- {
- case 1:
- {
- long aProcID;
- SFReply macSFReply;
- FSSpec new;
- Point where;
- SFTypeList typeList;
-
- typeList[0]='TEXT'; /* file type to search for */
- typeList[1]='ŎŎ'; /* file type to search for */
- where.h=20; where.v=90; /* SF dialog window position */
-
- SFGetFile(where, /* put topLeft corner of dialog box here */
- (ConstStr255Param)0, /* always 0 on SFGetFile() */
- (ProcPtr)0, /* address of custom filter */
- -1, /* looking for all file types */
- typeList, /* start of list of file types */
- (ProcPtr)0, /* use standard dialog handler */
- &macSFReply); /* put results here */
-
- if ( macSFReply.good )
- {
- WDPBRec wdpb;
- wdpb.ioNamePtr = 0;
- wdpb.ioVRefNum = macSFReply.vRefNum;
- wdpb.ioWDIndex =0;
- wdpb.ioWDProcID =0;
- wdpb.ioWDVRefNum =0;
- PBGetWDInfoSync( &wdpb );
- new.parID = wdpb.ioWDDirID;
- new.vRefNum = wdpb.ioWDVRefNum;
- BlockMove(macSFReply.fName, new.name, macSFReply.fName[0]+1);
- BeginProcessing();
- ProcessDoc(&new);
- EndProcessing();
- }
- }
- break;
- case 2:
- {
- // crt_getargs();
- break;
- }
- case 3: _exit(0);
- case 4: debug = !debug; break;
- }
- break;
-
- }
- }
- /* end HandleMenu */
-
- #ifdef PROGRESSWINDOW
-
- void SetUpWindow(void)
-
- {
- dragRect = qd.screenBits.bounds;
-
- progressWindow = NewWindow(0L, &windowBounds, (unsigned char *)"\010progress",
- true, noGrowDocProc, (WindowPtr) -1L, true, 0);
- SetPort(progressWindow);
- TEH = TENew(&windowBounds, &windowBounds);
- }
- /* end SetUpWindow */
-
- #endif
-
-
- void InitMacintosh(void)
-
- {
- MaxApplZone();
-
- InitGraf(&qd.thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
-
- }
- /* end InitMacintosh */
-
- #if 0
-
- void HandleMouseDown (EventRecord *theEvent)
-
- {
- WindowPtr theWindow;
- int windowCode = FindWindow (theEvent->where, &theWindow);
-
- switch (windowCode)
- {
- case inSysWindow:
- SystemClick (theEvent, theWindow);
- break;
-
- case inMenuBar:
- HandleMenu(MenuSelect(theEvent->where));
- break;
-
- #ifdef PROGRESSWINDOW
-
- case inDrag:
- if (theWindow == progressWindow)
- DragWindow(progressWindow, theEvent->where, &dragRect);
- break;
-
- case inContent:
- if (theWindow == progressWindow)
- {
- if (theWindow != FrontWindow())
- SelectWindow(progressWindow);
- else
- InvalRect(&progressWindow->portRect);
- }
- break;
-
- case inGoAway:
- if (theWindow == progressWindow &&
- TrackGoAway(progressWindow, theEvent->where))
- HideWindow(progressWindow);
- break;
- #endif
- }
- }
- /* end HandleMouseDown */
-
- void HandleEvent(void)
- {
- int ok;
- EventRecord theEvent;
-
- HiliteMenu(0);
- SystemTask (); /* Handle desk accessories */
-
- ok = GetNextEvent (everyEvent, &theEvent);
- if (ok)
- switch (theEvent.what)
- {
- case mouseDown:
- HandleMouseDown(&theEvent);
- break;
-
- case keyDown:
- case autoKey:
- if ((theEvent.modifiers & cmdKey) != 0)
- {
- HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
- }
- break;
-
- #ifdef PROGRESSWINDOW
-
- case updateEvt: if (progressWindow)
- {
- Rect upper = progressWindow->portRect;
- Rect lower = progressWindow->portRect;
- upper.bottom = upper.top + 32;
- lower.top = upper.bottom;
- BeginUpdate(progressWindow);
- SetPort(progressWindow);
- EraseRect(&lower);
- upper.right = lower.right;
- FillRect(&upper,(void *)&qd.white);
- TEUpdate(&progressWindow->portRect, TEH);
- EndUpdate(progressWindow);
- break;
- }
- case activateEvt:
- InvalRect(&progressWindow->portRect);
- break;
- #endif
-
- case kHighLevelEvent:
- AEProcessAppleEvent (&theEvent);
- break;
- }
- }
- /* end HandleEvent */
-
- #endif
- #ifdef PROGRESSWINDOW
-
- void UpdateFileName(FSSpec *new)
- {
- if (progressWindow)
- {
- Rect upper = progressWindow->portRect;
- Rect lower = progressWindow->portRect;
- upper.bottom = upper.top + 32;
- lower.top = upper.bottom;
- TESetText(&(new->name[1]),new->name[0],TEH);
- InvalRect(&lower);
- HandleEvent();
- }
- }
-
- #endif